export default function registerCondition(lf) {
  lf.register('condition', ({ PolygonNode, PolygonNodeModel, h }) => {
    class Node extends PolygonNode {
      getShape() {
        const { model } = this.props
        const { width, height, x, y, fillOpacity, strokeOpacity, points } =
          model
        const style = model.getNodeStyle()
        const transform = `matrix(1 0 0 1 ${x - width / 2} ${y - height / 2})`
        const pointsPath = points.map((point) => point.join(',')).join(' ')

        return h(
          'g',
          {
            transform,
          },
          [
            h('polygon', {
              ...style,
              points: pointsPath,
              strokeOpacity,
              fillOpacity,
            }),
          ]
        )
      }
    }
    class Model extends PolygonNodeModel {
      constructor(data, graphModel) {
        data.text = {
          value: (data.text && data.text.value) || '个儿童认为',
          x: data.x,
          y: data.y,
        }
        super(data, graphModel)
        const lenght = 35
        this.points = [
          [lenght, 0],
          [lenght * 2, lenght],
          [lenght, lenght * 2],
          [0, lenght],
        ]
        this.properties = data.properties || {}
      }

      getNodeStyle() {
        const style = super.getNodeStyle()
        const { properties } = this
        const { nodeStyle } = properties
        if (properties && nodeStyle) {
          if (nodeStyle.fill) {
            style.fill = nodeStyle.fill
          }
          if (nodeStyle.stroke) {
            style.stroke = nodeStyle.stroke
          }
        }

        return style
      }
    }
    return {
      view: Node,
      model: Model,
    }
  })
}
